package com.cxl.demo.controller;

import com.cxl.demo.service.LogisticsService;
import com.cxl.demo.util.TimeUpd;
import com.cxl.demo.vo.Logistics;
import com.cxl.demo.vo.ReturnData;
import com.cxl.demo.vo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/logistics")
@CrossOrigin
public class LogisticsController {

    @Autowired
    User user;
    @Autowired
    ReturnData returnData;
    @Autowired
    LogisticsService logisticsService;

    @PostMapping("Add")
    public ReturnData Add(@RequestBody Map<String,Object> map){
        returnData.setData(null);
       // Date date= new Date();
        String timestamp = new TimeUpd().getDate();
        Logistics logistics=new Logistics();
        logistics.setBuyHistoryId((Integer) map.get("buyHistoryId"));
        logistics.setCreateTime(timestamp);
        logistics.setContent((String) map.get("content"));
        Integer ok=logisticsService.Add(logistics);
        if(ok==1){
            returnData.setStatus(true);
            returnData.setMsg("更新物流成功");
        }else{
            returnData.setStatus(false);
            returnData.setMsg("物流信息不可重复");
        }

        return returnData;
    }

    @PostMapping("getLogistics")
    public ReturnData getLogistics(@RequestBody Map<String,Object> map){
        returnData.setData(null);
        returnData.setStatus(true);
        returnData.setMsg("查询成功");
        List<String> list=logisticsService.getLogistics();
        List<Logistics> logistics=new ArrayList<>();
        for(String item:list){
            Logistics logistics1=new Logistics();
            logistics1.setContent(item);
            logistics.add(logistics1);
        }


        returnData.setData(logistics);
        return returnData;
    }

    @PostMapping("getHistoryList")
    public ReturnData getHistoryList(@RequestBody Map<String,Object> map){
        returnData.setData(null);
        returnData.setStatus(true);
        returnData.setMsg("查询成功");
        returnData.setData(logisticsService.getHistoryList((Integer) map.get("buyHistoryId")));
        return returnData;
    }

}
